package operation;

import book.Book;
import book.Booklist;

public class DelOperation implements IOperation{
    public void work(Booklist booklist){
        System.out.println("删除图书！");
        //1、根据书名找到书的位置
        System.out.println("请输入你要删除的图书的名字：");
        String name=scanner.nextLine();
        int currentSize=booklist.getUsedSize();
        int index=0;
        int i=0;
        for (; i < currentSize; i++) {
            Book book=booklist.getPos(i);
            if (book.getName().equals(name)){
               index=i;
               break;
            }
        }

        if (i>=currentSize){
            System.out.println("没有你要删除的这本书！");
            return;
        }
        //2、进行删除
        for (int j = index; j < currentSize-1; j++) {
            Book book=booklist.getPos(j+1);
            booklist.setBook(j,book);
        }
        booklist.setBook(currentSize,null);
        booklist.setUsedSize(currentSize-1);
        System.out.println("删除成功！");
    }
}


















